<?php
/**
 * Created by JetBrains PhpStorm.
 * User: shauan
 * Date: 17/05/13
 * Time: 22:56
 * To change this template use File | Settings | File Templates.
 */
App::uses('AppController','Controller');
class IndexsController extends AppController
{
    public $helpers = array('Function','Session');
    public $uses = array('Usermgmt.User');

    public function index(){
        $this->layout = 'principal';
        $this->loadModel('Season');
        $userId=$this->UserAuth->getUserId();
        $user = $this->User->getNameById($userId);
        $options['conditions'] = array(
            'Anime.status'=>1,
            'Season.status'=>1,
        );
        $options['order'] = array(
          'Season.modified desc',
        );
        $seasons = $this->Season->find('all',$options);
        $menu_p = 'active';
        $this->set(compact('seasons','user','menu_p'));
        $this->set('title_for_layout','Principal');

    }

    public function updateEpisodes($id = null)
    {
        $this->layout = false;
        $this->loadModel('Season');
        $this->loadModel('Situation');
        $this->loadModel('Rating');

        if(!$this->Season->exists($id))
        {
            $this->render('/Errors/error404');
            return false;
        }
        if($this->request->is('post') || $this->request->is('put'))
        {
            $this->autoRender = false;
            if($this->Season->save($this->request->data,array('validate'=>false)))
            {
                $this->loadModel('Season');
                $options['conditions'] = array(
                    'Anime.status'=>1,
                    'Season.status'=>1,
                );
                $options['order'] = array(
                    'Season.modified desc',
                );
                $seasons = $this->Season->find('all',$options);
                $this->set(compact('seasons'));
                return $this->render('/Elements/listagemAnime');
            }
            else
            {
                return false;
            }
//            debug($this->Season->validationErrors);
        }
        else
        {
            $options = array('conditions'=>array('Season.'.$this->Season->primaryKey => $id));
            $this->request->data = $this->Season->find('first',$options);
            $situations = $this->Situation->find('list');
            $ratings = $this->Rating->find('list');
        }
        $this->set(compact('situations','ratings'));
        $this->set('title_for_layout','Atualizar Episodios');
    }

   public function InforAnime($id = null)
   {
       $this->layout = false;
       $this->LoadModel('Season');

       if(!$this->Season->exists($id))
       {
           throw new NotFoundException(__('Temporada não registrada no sistema.'));
       }

       $options['conditions'] = array(
           'Season.'.$this->Season->primaryKey=>$id,
       );
       $inforanime = $this->Season->find('first',$options);
       $title_for_layout = '+ Informações';
       $this->set(compact('inforanime','title_for_layout'));
   }
}